package tool.workflow.fsSoft

import dao._
import implicits.Implicits._
import play.api.i18n.Messages
import shared.Pojo.AllPlotData
import shared.pojo.WorkflowMission.ParentNodesData
import tool.Pojo.{CommandData, MyDao}
import tool.plot.{Venn, ZScore}
import tool.{Pojo, Tool, plot}
import tool.softT._
import tool.workflow.fsSoft.AllFsSoft.FSSoftBT

import java.io.File
import scala.collection.immutable.SeqMap
import upickle.default._

/**
 * Created by yz on 3/12/2020
 */
object Intersect extends FSSoftBT with IntersectSoftBT {


  override def produceInput(workspaceDir: File, resultDir: File, index: Int, parentNodeStr: String, id: String)(
    implicit dao: MyDao, messages: Messages): Unit = {
    val finalDiffMetabolites = parentNodeStr.mySplit(";").map { idStr =>
      val diffDir = new File(workspaceDir.getParentFile, idStr)
      Tool.getDiffMetaboliteFile(diffDir).csvLines.drop(1).map(_.head)
    }.reduceLeft { (x, y) =>
      x.intersect(y)
    }
    ("Metabolite" :: finalDiffMetabolites).toFile(Tool.getDiffMetaboliteFile(workspaceDir))
    val parentNodes = parentNodeStr.mySplit(";")
    val showNames = parentNodes.map { x =>
      Tool.getTrueOutDir(x)
    }
    val parentNodesData = ParentNodesData(parentNodes, showNames = showNames)
    write(parentNodesData).toFile(Tool.getCalculateConfigJsonFile(workspaceDir))
    ("ClassNote" :: parentNodesData.showNames).toFile(Tool.getGroupFile(workspaceDir))
  }

  override def getCommandData(workspaceDir: File, resultDir: File, index: Int): List[Pojo.CommandData] = {
    val solutionFile = new File(Tool.dataDir, "color_solution/color_solution_1.txt")
    val groupFile = new File(workspaceDir, "group.txt")
    val command =
      s"""
         |${Tool.rScript} ${new File(Tool.rPath, "produce_sample_color.R").unixPath} --g ${groupFile.unixPath}  --solution ${solutionFile.unixPath}
         |${Tool.rScript} ${Tool.rPath.unixPath}/fill_sample_color.R --g ${groupFile.unixPath}  --solution ${solutionFile.unixPath} --pc ${plot.FsVenn.plotO.configFileName}
           """.stripMargin
    val plotCommands = List(
      plot.FsVenn.getCommand(workspaceDir, "")
    )
    List(CommandData(workspaceDir, List(command):::plotCommands))
  }

  override def producePlotConfigFile(workspaceDir: File, index: Int): Any = {
    plot.FsVenn.initConfigFile(workspaceDir, "")
  }


}
